package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.EventListData;
import com.lzx.iteam.bean.TalkListData;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by shijunting on 15-5-25.
 */
public class SendTalkMsg extends CcMsgStructure {


    private Message mmCallback;
    private Context mContext;

    public SendTalkMsg(Message callback, Context context) {
        mmCallback = callback;
        mContext = context;
    }

    @Override
    public void onReceive(JSONObject data) {

        TalkListData talkListData = new TalkListData();
        try {
            JSONObject item = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            talkListData.createTime = item.getString("create_time");
            talkListData.talkId = item.getString("talk_id");
            talkListData.text = item.getString("text");
            // 人脉圈发送成功，记录 talkid
            Constants.NEW_TALK_ID = talkListData.talkId;
            if (item.has("content")) {
                StringBuilder thumbSb = new StringBuilder();
                StringBuilder oriSb = new StringBuilder();
                JSONArray content = item.getJSONArray("content");
                for (int i = 0; i < content.length(); i++) {
                    JSONObject jsonObject = content.getJSONObject(i);
                    if (jsonObject.has("thumb")) {
//                                Bitmap imageBitmap = ((BitmapDrawable) DownloadImageTask.loadImageFromNetwork(imageObject.getString("thumb"))).getBitmap();
//                                String imagePath = PhotoUtil.saveBitmaptoSdCard(imageBitmap, PhotoUtil.FOLDER_IMAGES_THUMBNAILS);
                        thumbSb.append(jsonObject.getString("thumb") + ",");
                    }
                    if (jsonObject.has("ori")) {
                        oriSb.append(jsonObject.getString("ori") + ",");
                    }
                    if (thumbSb.length() > 0) {
                        talkListData.thumbImage = thumbSb.substring(0, thumbSb.length() - 1);
                    }
                    if (oriSb.length() > 0) {
                        talkListData.oriImage = oriSb.substring(0, oriSb.length() - 1);
                    }
                }
            }
        } catch (
                JSONException e
                )

        {
            e.printStackTrace();
        }

        if (mmCallback != null)

        {
            mmCallback.obj = talkListData;
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }
    }
}
